console.log('Background script loaded');

chrome.runtime.onInstalled.addListener(() => {
  console.log('Extension installed');
});

// 监听所有对 Gitee API 的请求
chrome.webRequest.onBeforeRequest.addListener(
  (details) => {
    console.log('Gitee API 请求:', details);
  },
  { urls: ["https://gitee.com/api/*"] }
);

// 监听请求响应
chrome.webRequest.onCompleted.addListener(
  (details) => {
    console.log('Gitee API 响应:', details);
  },
  { urls: ["https://gitee.com/api/*"] }
);

// 修改 content script 注入逻辑
chrome.tabs.onUpdated.addListener(async (tabId, changeInfo, tab) => {
  if (changeInfo.status === 'complete' && tab.url.includes('excalidraw.com')) {
    try {
      // 尝试注入 content script
      await chrome.scripting.executeScript({
        target: { tabId: tabId },
        files: ['content.js']
      });
      console.log('Content script 注入成功');
    } catch (err) {
      // 如果是因为脚本已经存在而失败，这是正常的
      console.log('Content script 可能已存在:', err);
    }
  }
}); 